from __future__ import print_function
from __future__ import division

import sys
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn2pmml.decoration import ContinuousDomain
from sklearn2pmml.pipeline import PMMLPipeline
from sklearn2pmml.feature_extraction.text import Splitter
from sklearn2pmml import sklearn2pmml
from sklearn_pandas import DataFrameMapper
from sklearn.model_selection import GridSearchCV, StratifiedKFold
from sklearn.model_selection import train_test_split


if __name__ == '__main__':
    C_OPS = [10]

    param_grid = [
        {
            'classify__C': C_OPS
        }
    ]

    cv = StratifiedKFold(n_splits=10, shuffle=True)

    target = []
    X = []

    cols = ['yz_uid', 'age', 'gender', 'email', 'country', 'province', 'city', 'yz_uid', 'avg_monthly_consum_amt',
            'avg_monthly_order_cnt', 'last_3d_order_cnt', 'last_7d_order_cnt', 'last_15d_order_cnt',
            'last_30d_order_cnt', 'last_90d_order_cnt', 'last_180d_order_cnt', 'last_3d_order_amt', 'last_7d_order_amt',
            'last_15d_order_amt', 'last_30d_order_amt', 'last_90d_order_amt', 'last_180d_order_amt',
            'last_3d_order_unit_amt', 'last_7d_order_unit_amt', 'last_15d_order_unit_amt', 'last_30d_order_unit_amt',
            'last_90d_order_unit_amt', 'last_180d_order_unit_amt', 'is_last_3d_order', 'is_last_7d_order',
            'is_last_15d_order', 'is_last_30d_order', 'is_last_90d_order', 'is_last_180d_order', 'last_3d_goods_cnt',
            'last_7d_goods_cnt', 'last_15d_goods_cnt', 'last_30d_goods_cnt', 'last_90d_goods_cnt',
            'last_180d_goods_cnt', 'last_3d_team_cnt', 'last_7d_team_cnt', 'last_15d_team_cnt', 'last_30d_team_cnt',
            'last_90d_team_cnt', 'last_180d_team_cnt', 'last_3d_class1_cnt', 'last_7d_class1_cnt',
            'last_15d_class1_cnt', 'last_30d_class1_cnt', 'last_90d_class1_cnt', 'last_180d_class1_cnt',
            'last_3d_avg_daily_order_cnt', 'last_7d_avg_daily_order_cnt', 'last_15d_avg_daily_order_cnt',
            'last_30d_avg_daily_order_cnt', 'last_90d_avg_daily_order_cnt', 'last_180d_avg_daily_order_cnt',
            'top3_ai_class1_list', 'dndq_order_amt', 'dndq_order_cnt', 'dndq_goods_cnt', 'dndq_team_cnt',
            'hfcz_order_amt', 'hfcz_order_cnt', 'hfcz_goods_cnt', 'hfcz_team_cnt', 'hnwy_order_amt', 'hnwy_order_cnt',
            'hnwy_goods_cnt', 'hnwy_team_cnt', 'jjjz_order_amt', 'jjjz_order_cnt', 'jjjz_goods_cnt', 'jjjz_team_cnt',
            'myyp_order_amt', 'myyp_order_cnt', 'myyp_goods_cnt', 'myyp_team_cnt', 'nnfs_order_amt', 'nnfs_order_cnt',
            'nnfs_goods_cnt', 'nnfs_team_cnt', 'qt_order_amt', 'qt_order_cnt', 'qt_goods_cnt', 'qt_team_cnt',
            'qcyp_order_amt', 'qcyp_order_cnt', 'qcyp_goods_cnt', 'qcyp_team_cnt', 'rybh_order_amt', 'rybh_order_cnt',
            'rybh_goods_cnt', 'rybh_team_cnt', 'shfw_order_amt', 'shfw_order_cnt', 'shfw_goods_cnt', 'shfw_team_cnt',
            'spcy_order_amt', 'spcy_order_cnt', 'spcy_goods_cnt', 'spcy_team_cnt', 'sjsm_order_amt', 'sjsm_order_cnt',
            'sjsm_goods_cnt', 'sjsm_team_cnt', 'tsyx_order_amt', 'tsyx_order_cnt', 'tsyx_goods_cnt', 'tsyx_team_cnt',
            'xlxb_order_amt', 'xlxb_order_cnt', 'xlxb_goods_cnt', 'xlxb_team_cnt', 'ydhw_order_amt', 'ydhw_order_cnt',
            'ydhw_goods_cnt', 'ydhw_team_cnt', 'zbps_order_amt', 'zbps_order_cnt', 'zbps_goods_cnt', 'zbps_team_cnt',
            'top50_goods_title']

    feature_cols = [
        "avg_monthly_consum_amt",
        "avg_monthly_order_cnt",
        "last_3d_order_cnt",
        "last_7d_order_cnt",
        "last_15d_order_cnt",
        "last_30d_order_cnt",
        "last_90d_order_cnt",
        "last_180d_order_cnt",
        "last_3d_order_amt",
        "last_7d_order_amt",
        "last_15d_order_amt",
        "last_30d_order_amt",
        "last_90d_order_amt",
        "last_180d_order_amt",
        "last_3d_order_unit_amt",
        "last_7d_order_unit_amt",
        "last_15d_order_unit_amt",
        "last_30d_order_unit_amt",
        "last_90d_order_unit_amt",
        "last_180d_order_unit_amt",
        "is_last_3d_order",
        "is_last_7d_order",
        "is_last_15d_order",
        "is_last_30d_order",
        "is_last_90d_order",
        "is_last_180d_order",
        "last_3d_goods_cnt",
        "last_7d_goods_cnt",
        "last_15d_goods_cnt",
        "last_30d_goods_cnt",
        "last_90d_goods_cnt",
        "last_180d_goods_cnt",
        "last_3d_team_cnt",
        "last_7d_team_cnt",
        "last_15d_team_cnt",
        "last_30d_team_cnt",
        "last_90d_team_cnt",
        "last_180d_team_cnt",
        "last_3d_class1_cnt",
        "last_7d_class1_cnt",
        "last_15d_class1_cnt",
        "last_30d_class1_cnt",
        "last_90d_class1_cnt",
        "last_180d_class1_cnt",
        "last_3d_avg_daily_order_cnt",
        "last_7d_avg_daily_order_cnt",
        "last_15d_avg_daily_order_cnt",
        "last_30d_avg_daily_order_cnt",
        "last_90d_avg_daily_order_cnt",
        "last_180d_avg_daily_order_cnt",
        # "top3_ai_class1_list",
        "dndq_order_amt",
        "dndq_order_cnt",
        "dndq_goods_cnt",
        "dndq_team_cnt",
        "hfcz_order_amt",
        "hfcz_order_cnt",
        "hfcz_goods_cnt",
        "hfcz_team_cnt",
        "hnwy_order_amt",
        "hnwy_order_cnt",
        "hnwy_goods_cnt",
        "hnwy_team_cnt",
        "jjjz_order_amt",
        "jjjz_order_cnt",
        "jjjz_goods_cnt",
        "jjjz_team_cnt",
        "myyp_order_amt",
        "myyp_order_cnt",
        "myyp_goods_cnt",
        "myyp_team_cnt",
        "nnfs_order_amt",
        "nnfs_order_cnt",
        "nnfs_goods_cnt",
        "nnfs_team_cnt",
        "qt_order_amt",
        "qt_order_cnt",
        "qt_goods_cnt",
        "qt_team_cnt",
        "qcyp_order_amt",
        "qcyp_order_cnt",
        "qcyp_goods_cnt",
        "qcyp_team_cnt",
        "rybh_order_amt",
        "rybh_order_cnt",
        "rybh_goods_cnt",
        "rybh_team_cnt",
        "shfw_order_amt",
        "shfw_order_cnt",
        "shfw_goods_cnt",
        "shfw_team_cnt",
        "spcy_order_amt",
        "spcy_order_cnt",
        "spcy_goods_cnt",
        "spcy_team_cnt",
        "sjsm_order_amt",
        "sjsm_order_cnt",
        "sjsm_goods_cnt",
        "sjsm_team_cnt",
        "tsyx_order_amt",
        "tsyx_order_cnt",
        "tsyx_goods_cnt",
        "tsyx_team_cnt",
        "xlxb_order_amt",
        "xlxb_order_cnt",
        "xlxb_goods_cnt",
        "xlxb_team_cnt",
        "ydhw_order_amt",
        "ydhw_order_cnt",
        "ydhw_goods_cnt",
        "ydhw_team_cnt",
        "zbps_order_amt",
        "zbps_order_cnt",
        "zbps_goods_cnt",
        "zbps_team_cnt"
    ]

    data_file = '/Users/hardy/data/gender_age.csv'
    X = []
    with open(data_file) as fd:
        for l in fd:
            d = l.strip().split('\001')
            if len(d) != len(cols):
                continue
            X.append([float(x) if c in feature_cols else x for c, x in zip(cols, d)])

    data = pd.DataFrame(X, columns=cols)

    train, test = train_test_split(data, test_size=0.20, random_state=30)
    # data.columns = cols

    mapper = DataFrameMapper([
        (feature_cols, [ContinuousDomain()]),
    ])
    pipeline = PMMLPipeline([
        ("mapper", mapper),
        ('classify', LogisticRegression(penalty='l1', multi_class='ovr', max_iter=10))
    ])

    grid = GridSearchCV(pipeline, cv=cv, n_jobs=3, param_grid=param_grid)

    grid.fit(train, train['gender'])
    pipeline = grid.best_estimator_

    predicts = pipeline.predict(test)
    acc = sum(predicts == test['gender']) / len(predicts)
    print(acc)
    print(sum(predicts == '2'))
    print(sum(predicts == '1'))
    sklearn2pmml(pipeline, '/home/hardy/lr.pmml', with_repr=True, debug=True)

